//
//  AppDelegate.swift
//  xinlangweibo
//
//  Created by yangshuo on 15/11/22.
//  Copyright © 2015年 yangshuo. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        //实例化一个window等于mainscreen大小
        setupApperance()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = deafaultRootController
        window?.makeKeyAndVisible()
    //  print(UserAccountViewModel.sharedUserAccount.account)
        
        //监听通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwithchRootControllerNotification,//通知名称,通知中心用来识别通知
            object: nil, //发布通知的对象(发布WBSWITch的通知),如果nil,监听任何对象
            queue: nil)     //线程:nil为主线程
            {[weak self](notification) -> Void in //通知实现的方法
//                print(notification)
//                print(NSThread.currentThread())
                let vc = notification.object != nil ? WelcomeViewController() : MainViewController()
              self?.window?.rootViewController = vc
        }
        
            return true
    }
    //注销通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self,//监听者
            name: WBSwithchRootControllerNotification,  //监听的通知
            object: nil)                              //发送通知的对象
    }
    //设置全局外观, 所有全局外观的控件需要写在AppDelegate中
    private func setupApperance(){
        //导航栏渲染全局外观
        UINavigationBar.appearance().tintColor = NavigationBarTintColor//赋值的条件最好不要设置
        UITabBar.appearance().tintColor = TabbarTintColor
    }

}
// MARK: - 根控制器切换
extension AppDelegate{
  //判断程序的根控制器
    private var deafaultRootController: UIViewController{
        //判断用户是否登录
        if UserAccountViewModel.sharedUserAccount.userLoad{
            //如果用户登录,判断是否是新版本,如果是,跟控制器为新特性
            //如果否,根控制器为欢迎界面
            return isNewVersion ? NewFeatureCollectionViewController() : WelcomeViewController()
        }
        //如果用户未登录,显示登录界面
        return MainViewController()
    }
    private var isNewVersion: Bool{
        //1.当前版本
       // print(NSBundle.mainBundle().infoDictionary)
        //在可选的字典或者数组里面取东西,最先应该解包
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //强行解包,为它负责,它一定可以转成小数
        let version = Double(currentVersion)!
        
        print("当前版本\(version)")
   //2.之前版本,保存用户设置偏好
        
        let sandBoxKey = "sandBoxKey"
        //保存double类型键值
       let sandBoxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandBoxKey)
        //当sandBox时为空时打印0.0
        print("之前版本\(sandBoxVersion)")
    //3.保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: sandBoxKey)
        
        return version > sandBoxVersion
    }
}

